package com.xywh.wanandroid.compose_ui.main

import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.xywh.wanandroid.data.model.bean.ArticleItem
import com.xywh.wanandroid.event.EventViewModel.Companion.isLogin
import com.xywh.wanandroid.ext.showToastMsg


@Composable
fun ArticleItem(
    itemArticle: ArticleItem,
    isTop: Boolean,
    favoriteOnClick: (Int, Boolean) -> Unit,
    itemOnClick: (title: String, link: String) -> Unit
) {
    ListItem(
        modifier = Modifier.clickable {
            itemOnClick.invoke(itemArticle.title ?: "", itemArticle.link ?: "")
        },
        headlineContent = {
            Text(
                itemArticle.title ?: "",
                fontSize = 14.sp,
                color = Color.Black,
                modifier = Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth(),
            )
        },
        overlineContent = {
            Row(Modifier.fillMaxWidth()) {

                if (isTop) {
                    Text(
                        "置顶",
                        fontSize = 10.sp,
                        color = Color(0xFFF44336),
                        modifier = Modifier
                            .padding(end = 5.dp)
                            .border(
                                0.5.dp,
                                color = Color(0xFFF44336),
                                shape = RoundedCornerShape(10)
                            )
                            .padding(5.dp, 2.dp)
                    )
                }
                if (itemArticle.fresh) {
                    Text(
                        "新",
                        fontSize = 10.sp,
                        color = Color(0xFFF44336),
                        modifier = Modifier
                            .padding(end = 5.dp)
                            .border(
                                0.5.dp,
                                color = Color(0xFFF44336),
                                shape = RoundedCornerShape(10)
                            )
                            .padding(5.dp, 2.dp)
                    )
                }
                if (!itemArticle.tags.isNullOrEmpty()) {
                    Text(
                        itemArticle.tags[0].name ?: "",
                        fontSize = 10.sp,
                        color = Color(0xFF37CADD),
                        modifier = Modifier
                            .padding(end = 5.dp)
                            .border(
                                0.5.dp,
                                color = Color(0xFF37CADD),
                                shape = RoundedCornerShape(10)
                            )
                            .padding(5.dp, 2.dp)
                    )
                }



                Text(
                    itemArticle.author.ifEmpty { itemArticle.shareUser ?: "" },
                    fontSize = 10.sp,
                    color = Color(0xFF747474),
                    modifier = Modifier
                        .align(Alignment.Bottom)
                )
                itemArticle.niceDate?.let {
                    Text(
                        it,
                        fontSize = 10.sp,
                        color = Color(0xFF747474),
                        modifier = Modifier
                            .padding(start = 5.dp)
                            .fillMaxWidth()
                            .align(Alignment.Bottom),
                        textAlign = TextAlign.End
                    )
                }

            }
        },
        supportingContent = {


            Row(
                Modifier
                    .padding(top = 10.dp)
                    .fillMaxWidth(),
            ) {

                Text(
                    "${itemArticle.superChapterName ?: ""}/${itemArticle.chapterName ?: ""}",
                    modifier = Modifier.weight(1f),
                    fontSize = 12.sp,
                    color = Color(0xFF787878)
                )

                Icon(
                    imageVector = if (itemArticle.collect) Icons.Filled.Favorite else Icons.Filled.FavoriteBorder,
                    modifier = Modifier
                        .size(18.dp)
                        .clickable {
                            if (isLogin.value) {
                                val collect = itemArticle.collect
                                itemArticle.collect = !itemArticle.collect
                                favoriteOnClick.invoke(itemArticle.id, collect)
                            } else {
                                showToastMsg("请先登录")
                            }

                        },
                    contentDescription = "Localized description",
                )
            }
        },
        //                        leadingContent = {
        //
        //                        },
        //                        trailingContent = {
        //
        //                        }
    )
    HorizontalDivider()
}
